package cn.bdqn.controller;

import cn.bdqn.domain.Order;
import cn.bdqn.service.OrderService;
import cn.bdqn.utils.result.Page;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 订单控制层
 * @author dkj
 */

@RestController
public class OrderController {


    /**
     * 创建日志记录器
     */
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OrderService orderService;



    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/order/query/{id}")
    public Result queryByPrimaryKey(@PathVariable("id") Integer id){

        try {
            logger.info("进入方法了，参数：id={}",id);
            //根据id查询
            Order order = orderService.queryByPrimaryKey(id);
            //返回结果
            return ResultGenerator.genSuccessResult(order);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("查询失败");
        }
    }


    /**
     * 查询全部 分页
     * @param currentPage  当前页
     * @param pageSize 每页显示数量
     * @param orderNumber 订单号
     * @param paymentType 付款类型
     * @param state 订单状态  1.处理  2.未处理
     * @return
     */
    @GetMapping("/order/query/{currentPage}/{pageSize}/{orderNumber}/{paymentType}/{state}")
    public Result queryByOrderNumberAndPaymentType(@PathVariable("currentPage") Integer currentPage,
                                                 @PathVariable("pageSize") Integer pageSize,
                                                 @PathVariable("orderNumber") String orderNumber,
                                                 @PathVariable("paymentType") Integer paymentType,
                                                 @PathVariable("state") Integer state){
        try {
            logger.info("进入了方法，参数：currentPage={},pageSize={},orderNumber={},paymentType={},state={}"
                    ,currentPage,pageSize,orderNumber,paymentType,state);
            //查询
            Page page = orderService.queryByOrderNumberAndPaymentType(currentPage, pageSize, orderNumber, paymentType, state);
            //返回结果和总记录数
            return ResultGenerator.genSuccessListResult(page.getMenuList(),page.getTotalRecord());
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("查询失败");
        }

    }


    /**
     * 新增订单
     * @param order
     * @return
     */
    @PostMapping("/order/save/")
    public Result saveOrder(@RequestBody Order order){

        try {
            logger.info("进入了方法，参数：order={}",order);
            //新增并返回对象
            Order order1 = orderService.saveOrder(order);
            //返回结果
            return ResultGenerator.genSuccessResult(order1);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("新增失败");
        }
    }



    /**
     * 修改
     * @param order
     */
    @PutMapping("/order/update/")
    public Result updateOrder(@RequestBody Order order){
        try {

            logger.info("进入了方法，参数:order={}",order);
            //修改订单
            orderService.updateOrder(order);
            return ResultGenerator.genFailResult("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("修改失败");
        }
    }


    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/order/delete/{id}")
    public Result deleteOrder(@PathVariable("id") Integer id){
        try {

            logger.info("进入了方法，参数：id={}",id);
            //根据id删除订单
            orderService.deleteOrder(id);
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("删除失败");
        }
    }
}
